package com.hc.controller;


import com.hc.bean.Pager;
import com.hc.bean.UserInfo;
import com.hc.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userinfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> getUserList(String userName, Integer page, Integer rows){
//        System.out.println("**********"+userName+":"+page+rows);
        Map<String,Object> resultMap=new HashMap<>();
        List<UserInfo> userInfoList=null;
        Pager pager=new Pager();
        pager.setPage(page);
        pager.setPageRows(rows);
        int userCount=userInfoService.getUserInfoCount(userName);
        if(userCount>0){
            userInfoList=userInfoService.getUserInfo(userName,pager);
//            System.out.println(userInfoList.size());
        }
        resultMap.put("total", userCount);
        resultMap.put("rows", userInfoList);
        return resultMap;
    }

    @RequestMapping("/setIsEnableUser")
    @ResponseBody
    public Map<String,Object> setIsEnableUser(String uids, Integer flag){
//        System.out.println("**********"+uids+":"+flag);
        uids = uids.substring(0, uids.length() - 1);
        Map<String,Object> resultMap=new HashMap<>();
        if (userInfoService.setIsEnableUser(flag,uids)){
            resultMap.put("success", "true");
            resultMap.put("message", "用户状态改成功!");
        }else {
            resultMap.put("success", "false");
            resultMap.put("message", "用户状态改失败!");
        }
        return resultMap;
    }

    @RequestMapping("/getValidUser")
    @ResponseBody
    public List<UserInfo> getValidUser(){
        List<UserInfo> userInfoList=userInfoService.getValidUser();
        UserInfo userInfo=new UserInfo();
        userInfo.setId(0);
        userInfo.setUserName("请选择客户名");
        userInfoList.add(0,userInfo);
        return userInfoList;
    }

    @RequestMapping("/getOrderUser")
    @ResponseBody
    public List<UserInfo> getOrderUser(){
        List<UserInfo> userInfoList=userInfoService.getOrderUser();
        UserInfo userInfo=new UserInfo();
        userInfo.setId(0);
        userInfo.setUserName("请选择客户名");
        userInfoList.add(0,userInfo);
        return userInfoList;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public Map<String,Object> addUser(UserInfo userInfo){
        Map resultMap=new HashMap();
        userInfo.setRegDate(new Date());
        if(userInfoService.addUser(userInfo)){
            resultMap.put("success", "true");
            resultMap.put("message", "用户添加成功!");
        }else {
            resultMap.put("success", "true");
            resultMap.put("message", "用户添加失败!");
        }
        System.out.println(userInfo);
        return resultMap;
    }
}
